package com.llz.leaveApply.controller;


import com.llz.leaveApply.dto.Result;
import com.llz.leaveApply.entity.LeaveApply;
import com.llz.leaveApply.entity.LeaveClearance;
import com.llz.leaveApply.service.LeaveApplyService;
import com.llz.leaveApply.service.LeaveClearanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author llz
 * @since 2023-01-21
 */
@RestController
@RequestMapping("/leaveApply/leave-clearance")
public class LeaveClearanceController {

    @Autowired
    LeaveClearanceService leaveClearanceService;
    @Autowired
    LeaveApplyService leaveApplyService;

    /**
     * 添加请假申请
     */
    @PostMapping("add")
    void addClearance(@RequestBody() LeaveClearance leaveClearance) {
//leaveClearance.setId(.);
        Boolean success = leaveClearanceService.save(leaveClearance);
        if (success) {
            //添加销假申请成功的话  将请假状态设为销假待审批
            LeaveApply leaveApply = new LeaveApply();
            leaveApply.setId(leaveClearance.getApplyId());
            leaveApply.setState(6);
            leaveApplyService.update();
        }
    }

///leaveApply/leave-clearance/getLastwaitForApproverclearance
    @PostMapping("/getLastwaitForApproverclearance")
    public Result getLastwaitForApproverclearance(@RequestParam("userNumber") String userNumber) {
//查一个审批人为当前用户且未审批的申请
        List<LeaveClearance> leaveClearanceList = leaveClearanceService.query().eq("approver", userNumber).
                eq("yishenpi", false).list();
        if (leaveClearanceList.size() > 0) {
            return Result.ok(leaveClearanceList.get(0));
        }
        return Result.ok();
    }

}